// 随机数
var timestamp=+Math.round(new Date().getTime()/1000);
var timestamps = 'ts='+timestamp;


$(document).ready(function () {
  
  $("form").submit(function(){
    return false;
  });
 $(".lv-link li.last").mouseover(function(){
    $(this).find("#appCode").show();
  })
  $(".lv-link li.last").mouseout(function(){
    $("#appCode").hide();
  })

  $("#appCode .codeBtn>button").click(function(){
    var url = $(this).attr("data-url");
        $('.codeImg img').attr("src",url);
  });
  if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){
    $("body").css('height','100%');
    $("#floatDiv").css('right','15px');
    $("#serviceDiv").css('right','15px');
  }

});

  /*二维码弹窗*/
$("#codeBox").toggle(function(){
    	$(".codeboximg").show();
			$(".Telephoneimg").hide();
  	},
    function(){
    	$(".codeboximg").hide();
			$(".Telephoneimg").show();
    }
);
$("#Telephone1").click(function(){
	$(".codeboximg").hide();
	$(".Telephoneimg").show();
});

function stopDefault(e) { 
    if (e && e.preventDefault) {//如果是FF下执行这个
        e.preventDefault();
    }else{ 
        window.event.returnValue = false;//如果是IE下执行这个
    }
    return false;
}

//获取url中的参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return decodeURI(r[2]); return null; //返回参数值
}


//获取form 的所有参数，组装成json 对象，可以用于ajax 请求
function getParams(form){
  var params = {};
  $(form).find("input").each(function(obj){
    if($(this).attr('disabled')=='disabled'){
      return true;
    }
    if($(this).attr('data-disabled')=='disabled'){
      return true;
    }
    var name = '';
    var value = '';
    if($(this).attr('type')=='radio'){
      name = $(this).attr("name");
      value = $(':radio[name="'+name+'"]:checked').val();
      params[name] = value;
      return true;
    }else{
      name = $(this).attr("name");
      value = $(this).val();
    }
    if(name){
      params[name] = value;
      
    }
    
  });
  $(form).find("select").each(function(obj){
    if($(this).attr('disabled')=='disabled'){
      return false;
    }
    if($(this).attr('data-disabled')=='disabled'){
      return true;
    }
    var name = $(this).attr("name");
    var value = $(this).val();
    params[name] = value;
  }); 
 $(form).find("textarea").each(function(obj){
    if($(this).attr('data-disabled')=='disabled'){
      return true;
    }
    var name = $(this).attr("name");
    var value = $(this).val();
    params[name] = value;
  }); 

  return params;
}
//清空表单数据的
function clearForm(form){
  $(':input',form).not(':button, :submit, :reset, :hidden, :textarea').val('').removeAttr('checked').removeAttr('selected');  
}

//当点击跳转链接后，回到页面顶部位置
$("#backTop").click(function(){
  $("body,html").animate({scrollTop:0},300);
});
//当点击跳转链接后，回到页面底部位置
$(".scroll-bottom").click(function(){
  if (isFirefox=navigator.userAgent.indexOf("Firefox")>0) {
    $("body").css('height','');
    var bottom = ($("body").height())*90/100;
    $("body,html").animate({scrollTop:bottom},300);
    $("body").css('height','100%');
  }else{
    var bottom = ($("body").height())*90/100;
    $("body,html").animate({scrollTop:bottom},300);
  }
  
});
/*用户中心下拉导航*/
$(".click_nav").click(function(){
  $(".buto").toggleClass("curr")
  $(".fore1_nav").slideToggle();
  return false;
});
/*用户中心下拉导航*/
$(".blog_buto").click(function(){
  $(".blog_buto").toggleClass("curr")
  $(".fore2_nav").slideToggle();
  return false;
});
$(".lv-link li").eq(4).css("margin-right","30px")
$(".lv-link li a").eq(6).css("color","#004799")

// 底部移动客户端显示跟隐藏
 $(".footer-icon1").mouseover(function(){
  $(".footer-code1").css("display","block")
});
$('.footer-icon1').mouseout(function(){
  $(".footer-code1").css("display","none")
});
  $(".footer-icon2").mouseover(function(){
  $(".footer-code2").css("display","block")
});
$('.footer-icon2').mouseout(function(){
  $(".footer-code2").css("display","none")
});
  $(".footer-icon3").mouseover(function(){
  $(".footer-code3").css("display","block")
});
$('.footer-icon3').mouseout(function(){
  $(".footer-code3").css("display","none")
});

//刷新token之后还是401跳到登录页面
$(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) {
  console.info(jqXHR.responseText);
  if (jqXHR.status == "401") {
        window.location.href = jumpUrl+'/login';
  }
});
  //刷新token
  var token = JSON.parse(localStorage.getItem('token'));
  if (token) {
    $.ajaxSetup({
      
      beforeSend: function(xhr, settings) {
        
          // 看看路径是否需要登录才可以访问
          if (!isPublicRoute(settings.url)) {
            // 检查看token是否已经过期
            var expired = isTokenExpired(token);
            if (expired) {
              // 如果token已经过期，则要更新token
              $.ajax({
                type: 'POST',
                url: jumpUrl+'/refresh_token',
                data: {},
                headers: {
                  'Authorization': 'Bearer '+token,
                  'Accept': 'application/json'
                },
                success: function(data){
                  console.info('successfully refreshed token', data);
                  token = data.data.token;
                  localStorage.setItem("token", JSON.stringify(token));
                },
                error: function(data){
                  window.location.href = jumpUrl+'/login';
                },
                async: false
              });
            }
            xhr.setRequestHeader('Authorization', 'Bearer '+token);
          }
      }
      // headers: {
      //   'Authorization': 'Bearer '+token
      // }
    });
  }

  // 退出登录
  $(".logout").click(function(){
    var tish_url = location.href;
    var loading = layer.msg('正在退出...', {icon: 16});
    $.post(logout_url,function(data){

    },'json').error(function(data){
      layer.msg("退出失败");

    }).success(function(data){
      layer.close(loading);
      layer.msg("退出成功");
      sessionStorage.removeItem("token");
      window.location.href = tish_url;
      
    });
  });

  $(document).on( "click", "#jq-register", function(e) {
    sessionStorage.setItem("goback", location.href);
  });

function isPublicRoute(route) {
  // 以下路径都不需要登录或者token进行访问
  var public_routes = [
    '/refresh_token', 
    '/login',
    '/logout',
    '/send-code',
    '/forgotpwd',
    '/question',
    '/knowledge/latest',
    '/news/list_news',
    '/knowledge/weekly',
    '/case/list_case',
    '/knowledge/article/list_articles',
    '/knowledge/document/list',
    '/knowledge/most_liked',
    '/lawyer/workscopes',
    '/lawyer/city',
    '/evaluate',
    '/area/province',
    '/lawyer/list_lawyers'
  ];
  var isPublic = false;
  $.each(public_routes, function( index, value ) {
      if (route.match(value)) {
          isPublic = true;
          return false;
      }
  });
  return isPublic;
}
initCompanyStore();
function initCompanyStore(){
	var nes_url = jumpUrl+"/api/index/card";
	$.get(nes_url,function(e){
    },"json").error(function(data){
        layer.msg("请求失败")
    }).success(function(data){
      $(".loading").remove()
      var obj = data.data;
      console.info(obj)
      localStorage.setItem("companyStore", JSON.stringify(obj));//保存到localStorage
    })
//	var companyStore = JSON.parse(localStorage.getItem("companyStore"));//localStorage获取出来
//	console.info(companyStore[0].title,"取")
}
